<?php


namespace app\api\controller;


use app\common\controller\Api;

class Orders extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';

    /**
     * @var $model \app\common\model\Orders
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Orders();
    }

    /**
     * 需求订单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function demand()
    {
        [$where, $sort, $order, $offset, $limit] = $this->buildparams($this->model, '', '', '');
        $total = $this->model
            ->where($where)
            ->where('aid','=', $this->auth->id)
            ->count();
        $list = $this->model
            ->where($where)
            ->where('aid','=', $this->auth->id)
            ->with('demand,goods')
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $result = array("total" => $total, "rows" => $list);
        $this->success('', $result);
    }

    /**
     * 服务订单
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goods()
    {
        [$where, $sort, $order, $offset, $limit] = $this->buildparams($this->model, '', '', '');
        $total = $this->model
            ->where($where)
            ->where('bid','=', $this->auth->id)
            ->count();
        $list = $this->model
            ->where($where)
            ->where('bid','=', $this->auth->id)
            ->with('demand,goods')
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $result = array("total" => $total, "rows" => $list);
        $this->success('', $result);
    }

    /**
     * 订单详情
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $id = input('param.id',0, 'intval');
        if (!$id || $id <=0){
            $this->error('参数异常');
        }
        if (!$row = $this->model->with('goods,demand,partyA,partyB.shop')->find($id)){
            $this->error('未找到该订单');
        }
        if ($row->bid != $this->auth->id && $row->aid != $this->auth->id){
            $this->error('无权限');
        }
        $this->success('', $row);
    }

    /**
     * 上传合同
     */
    public function uploadcontract()
    {
        $id = input('param.id',0, 'intval');
        $contract = input('param.contract','','trim');
        $result = $this->validate(['id'=>$id,'contract'=>$contract],[
            'id'  => 'require|gt:0',
            'contract'  => 'require|url'
        ]);
        if(true !== $result){
            $this->error($result);
        }
        if (!$row = $this->model::get($id)){
            $this->error('未找到该订单');
        }
        if ($row->aid != $this->auth->id && $row->bid != $this->auth->id){
            $this->error('您没有权限上传合同');
        }
        if ($row->status != 'purposed'){
            $this->error('该订单无法上传合同');
        }
        $row->contract = $contract;
        $row->status = 'signed';
        $row->verify_status = 'await';
        $row->save();
        $this->success('上传合同成功');
    }

    /**
     * 交付
     * @throws \think\exception\DbException
     */
    public function deliver()
    {
        $id = input('param.id',0, 'intval');
        if (!$id || $id <=0){
            $this->error('参数异常');
        }
        if (!$row = $this->model::get($id)){
            $this->error('未找到该订单');
        }
        if ($row->bid != $this->auth->id){
            $this->error('您没有权限交付订单');
        }
        if ($row->status != 'signed' || $row->verify_status != 'allow'){
            $this->error('暂不可交付');
        }
        $row->status = 'delivered';
        $row->save();
        $this->success('交付成功，等待甲方验收');
    }

    /**
     * 验收
     * @throws \think\exception\DbException
     */
    public function check()
    {
        $id = input('param.id',0, 'intval');
        if (!$id || $id <=0){
            $this->error('参数异常');
        }
        if (!$row = $this->model::get($id)){
            $this->error('未找到该订单');
        }
        if ($row->aid != $this->auth->id){
            $this->error('您没有权限验收订单');
        }
        if ($row->status != 'delivered' || $row->verify_status != 'allow'){
            $this->error('暂不可验收订单');
        }
        $row->status = 'checked';
        $row->verify_status = 'await';
        $row->save();
        $this->success('验收成功，等待审核');
    }


}